package org.lanqiao.lg.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.Education;
import org.lanqiao.bean.User;
import org.lanqiao.bean.Work;
import org.lanqiao.lg.dao.impl.TestDaoImpl;
import org.lanqiao.sjx.dao.impl.EducationImpl;
import org.lanqiao.sjx.dao.impl.UserImpl;
import org.lanqiao.sjx.dao.impl.WorkImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/TestServlet")
public class TestServlet extends BaseServlet {
	Gson gson = new Gson();
	TestDaoImpl testdao = new TestDaoImpl();
	UserImpl userdao = new UserImpl();
	EducationImpl edudao = new EducationImpl();
	WorkImpl workdao = new WorkImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");

		this.request = request;
		this.response = response;

		String key = getString("key");
		System.out.println(request.getSession().getId() + key + "/id=" + id + "/typeU=" + typeU);
		redata.setMsg("");
		if (!isNull("key")) {
			switch (key) {
			case "getRes":
				getRes();
				break;
			case "getUser":
				getUser();
				break;
			case "getUEdu":
				getEdu();
				break;
			case "getUW":
				getW();
				break;
			default:
				redata.setCode(3);
				redata.setMsg("key参数异常");
				redata.setData("");
				break;
			}
		} else {
			redata.setCode(3);
			redata.setMsg("key参数为空");
			redata.setData("");
		}
		System.out.println(redata.toString());
		response.getWriter().write(gson.toJson(redata));
	}
	
	//通过id获取用户教育信息
		private void getEdu() {
			Integer uid = getInt("uid");
			System.out.println(uid);
			if (redata.getMsg() != "") {
				redata.setCode(3);
				redata.setData("");
				return;
			}
			List<Education> edu = edudao.selectEducation(uid);
			System.out.println(edu);
			if (edu != null) {
				redata.setCode(2);
				redata.setMsg("selectResByPId2 成功");
				redata.setData(edu);
				return;
			} else {
				redata.setCode(3);
				redata.setMsg("服务器异常");
				redata.setData(edu);
				return;
			}
		}
		
		//通过id获取用户工作经验
		private void getW() {
			Integer uid = getInt("uid");
			System.out.println(uid);
			if (redata.getMsg() != "") {
				redata.setCode(3);
				redata.setData("");
				return;
			}
			List<Work> works = workdao.selectWork(uid);
			System.out.println(works);
			if (works != null) {
				redata.setCode(2);
				redata.setMsg("selectResByPId2 成功");
				redata.setData(works);
				return;
			} else {
				redata.setCode(3);
				redata.setMsg("服务器异常");
				redata.setData(works);
				return;
			}
		}

//通过id获取用户信息
	private void getUser() {
		Integer uid = getInt("uid");
		System.out.println(uid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		User User = userdao.selectUser(uid);
		if (User != null) {
			redata.setCode(2);
			redata.setMsg("selectResByPId2 成功");
			redata.setData(User);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(User);
			return;
		}
	}

// 通过id获取简历的信息
	private void getRes() {
		Integer rid = getInt("resId");
		System.out.println(rid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Map<String, Object>> rifno = testdao.selectResById(rid);
		if (rifno != null) {
			redata.setCode(2);
			redata.setMsg("selectResByPId2 成功");
			redata.setData(rifno);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(rifno);
			return;
		}

	}
}
